//获取历年变压器的台数和故障次数
var transmissionBarLine = new Echarts3LineBars("running");
var muxianBarLine = new Echarts3LineBars("muxian");

$(function() {
	initTransFaultQueryYear();
	initTransmissionFaultTabDatas();
	initBusbarQueryYear();
	initBusbarFaultTabDatas();
	var t1 = window.setInterval(function() {
		var pane1width = $('#pane1').width();
		if (pane1width != '0') {
//			$("#running").css("width", parseInt(pane1width) / 2);
//			$("#transformer").css("width", parseInt(pane1width) / 2);
//			$("#bianyaqi").css("width", parseInt(pane1width));
			transmissionBarLine.getChart().resize();
			window.clearInterval(t1);
		}
	}, 20);
});

function changewidth() {
	var t1 = window.setInterval(function() {
		var pane2width = $('#pane2').width();
		if(pane2width != '0') {
//			$("#muxian").css("width", parseInt(pane2width)/2);
//			$("#muxianpie").css("width", parseInt(pane2width)/2);
//			$("#fault-muxian").css("width", parseInt(pane2width)/2);
			muxianBarLine.getChart().resize();
			muxianfaultpie.getChart().resize();
			muxianfaultTypeCompanyBar.getChart().resize();
			window.clearInterval(t1);
		}
	}, 20);
	
}

//变压器故障分析tab页面所有数据
function initTransmissionFaultTabDatas() {
	var startYear = $("#firstYear").val();
	var endYear = $("#lastYear").val();
	var index = document.getElementById("queryForm:vol").selectedIndex;
	var voltage = document.getElementById("queryForm:vol").options[index].value;
	// 按照查询条件获取多年数据
	getTranstDataYears(startYear,endYear,voltage);
	// 仅查询某一年的数据
	getTransDataYear(startYear,voltage);
}

//按照查询条件获取多个年份的变压器故障数据
function getTranstDataYears(startYear,endYear,voltage) {
	$.getJSON('/rest/transmission/getTransmissionFaultCountData/'+startYear+'/'+endYear+'/'+voltage,function(result) {
		result.sort(by("name"));
		getTransmissionBarsLine(result);
	});
}

// 获取某一年的变压器故障数据
function getTransDataYear(year,voltage) {
	$.getJSON('/rest/transmission/getTransFaultByYearAndVoltage/'+ year +'/'+ voltage, function(result) {
		getTransmissionPie(result);
		getTransmissionBar(result);
	});
}

//母线故障分析tab页面所有数据
function initBusbarFaultTabDatas() {
	var startYear = $("#firstYear1").val();
	var endYear = $("#lastYear1").val();
	var index = document.getElementById("queryForm1:vol1").selectedIndex;
	var voltage = document.getElementById("queryForm1:vol1").options[index].value;
	// 按照查询条件获取多年数据
	getBusbarDataYears(startYear,endYear,voltage);
	// 仅查询某一年的数据
	getBusbarDataYear(startYear,voltage);
}

//按照查询条件获取多个年份的母线故障数据
function getBusbarDataYears(startYear,endYear,voltage) {
	$.getJSON('/rest/transmission/getBusbarFaultCountData/'+startYear+'/'+endYear+'/'+voltage,function(result) {
		result.sort(by("name"));
		getMuxianBarsLine(result);
	});
}

//查询某一年的母线故障类型数据
function getBusbarDataYear(year,voltage) {
	$.getJSON('/rest/transmission/getBusbarFaultByYearAndVoltage/'+year+'/'+voltage,function(result) {
		getMuXianPie(result);
		getMuXianBar(result);
	});
}

// 初始化变压器故障分析Tab页时间
function initTransFaultQueryYear() {
	var d = new Date();
	var currentYear = d.getFullYear();
	$("#firstYear").val(currentYear-5);
	$("#firstYear").datetimepicker({
		startView:4,
		minView:4,
		autoclose:true,
		format:'yyyy'
	});
	$("#lastYear").val(currentYear);
	$("#lastYear").datetimepicker({
		autoclose:true,
		startView:4,
		minView:4,
		format:'yyyy'
	});
}

// 初始化母线故障分析Tab页时间
function initBusbarQueryYear() {
	var d = new Date();
	var currentYear = d.getFullYear();
	$("#firstYear1").val(currentYear-5);
	$("#firstYear1").datetimepicker({
		startView:4,
		minView:4,
		autoclose:true,
		format:'yyyy'
	});
	$("#lastYear1").val(currentYear);
	$("#lastYear1").datetimepicker({
		autoclose:true,
		startView:4,
		minView:4,
		format:'yyyy'
	});
}

//按照年升序排列
function yearSortAsc(a,b) {
	return a - b;
}
//按照年升序排列
var by = function(name) {
	 return function(o, p){
	   var a, b;
	   if (typeof o === "object" && typeof p === "object" && o && p) {
	     a = o[name];
	     b = p[name];
	     if (a === b) {
	       return 0;
	     }
	     if (typeof a === typeof b) {
	       return a < b ? -1 : 1;
	     }
	     return typeof a < typeof b ? -1 : 1;
	   }
	   else {
	     throw ("error");
	   }
	}
}

//变压器柱状图加折线图		
function getTransmissionBarsLine(datas) {
	var yrightname = '年故障率';
	var yleftname = '变压器台数';
	var yaddname = '新增台数';
	var legendLabels = [ '变压器台数', '新增台数', '年故障率' ];
	var ratios = [];
	//增加数
	var add = [];
	//故障次数
	var faultNum = [];
	//总数
	var totalNum = [];
	// 年份，x轴数据
	var years = [];
	for ( var i in datas) {
		for ( var j in datas[i].value) {
			if (datas[i].value[j].name == "变压器台数") {
				totalNum.push(datas[i].value[j].value);
			}
			if (datas[i].value[j].name == "新增台数") {
				add.push(datas[i].value[j].value);
			}
			if (datas[i].value[j].name == "故障次数") {
				faultNum.push(datas[i].value[j].value);
			}
		}
		years.push(datas[i].name);
	}
	for (var i = 0; totalNum.length > i; i++) {
		var ratio = 0;
		if(0!=totalNum[i]){
			ratio = (parseInt(faultNum[i]) / parseInt(totalNum[i])) * 100;
		}
		ratios.push(ratio.toFixed(2));
	}
	var start = $("#firstYear").val();
	var end = $("#lastYear").val();
	$("#bianyaqidate").empty();
	$("#bianyaqidate").prepend(start + "-" + end + "年南方电网变压器运行情况");
	if ($("#yeardate").empty()) {
		$("#yeardate").append(start + "年变压器故障类型分析");
	} else {
		$("#yeardate").append("");
	}
	if ($("#electbarid").empty()) {
		$("#electbarid").append(start + "年南方电网变压器故障类型统计");
	} else {
		$("#electbarid").append("");
	}
	
	transmissionBarLine.setLeftyAxisTitle('台数');
	transmissionBarLine.setRightyAxisTitle('年故障率（次/百台·年）');
	transmissionBarLine.setLegendLabels(legendLabels);
	transmissionBarLine.setNumber(totalNum);
	transmissionBarLine.setRatio(ratios);
	transmissionBarLine.setYears(years);
	transmissionBarLine.setYleftname(yleftname);
	transmissionBarLine.setYrightname(yrightname);
	transmissionBarLine.setYaddname(yaddname);
	transmissionBarLine.setAdd(add);
	transmissionBarLine.show();
}
//饼状图
function getTransmissionPie(datas) {
	if(datas[0].faultType.length==0) {
		$("#transformer").html("该时间暂无数据信息");
	} else {
		var faultpie = new Pie("transformer");
		var faultTypeData = [];
		for ( var i in datas[0].faultType) {
			var num = [];
			num.push(datas[0].faultType[i]);
			num.push(0);
			faultTypeData.push(num);
		}
		for ( var i in faultTypeData) {
			for (var j = 1; datas.length > j; j++) {
				faultTypeData[i][1] += parseInt(datas[j].value[i]);
			}
		}
		var option = faultpie.getOption();
		option.legend.orient = 'vertical';
		faultpie.setLegendPosition('left', 'left');
		faultpie.setPie(faultTypeData);
		faultpie.setPieOuterRadius('50%');
		faultpie.show();
	}
}
//变压器故障类型统计
function getTransmissionBar(datas) {
	if(datas[0].faultType.length==0) {
		$("#bianyaqi").html("该时间暂无数据信息");
	} else {
		var faultTypeCompanyBar = new CategoryBar('bianyaqi');
		var faultTypeData = {
			name : '',
			xData : {
				name : '单位',
				value : []
			},
			yData : {
				name : '次数',
				value : []
			}
		};
		for (var i = 1; datas.length > i; i++) {
			faultTypeData.xData.value.push(datas[i].name);
		}
		;
		for ( var j in datas[0].faultType) {
			var obj = {};
			obj["lineValue"] = [];
			obj["lineName"] = datas[0].faultType[j];
			for (var i = 1; datas.length > i; i++) {
				obj["lineValue"].push(datas[i].value[j]);
			}
			faultTypeData.yData.value.push(obj);
		}
		
		var opt = faultTypeCompanyBar.getOption();
		opt.tooltip.axisPointer.type = "shadow";
		opt.grid.bottom = 40;
		opt.legend.top = 10;
		opt.grid.top = '30%'; // 图形位置
		opt.title.textStyle.fontSize = 16;
		opt.title.textStyle.fontWeight = 'normal';
		faultTypeCompanyBar.setTitlePosition('center').setDataZoomStartValue(0, 10);
		faultTypeCompanyBar.setData(faultTypeData);
		faultTypeCompanyBar.show();
	}
}

transmissionBarLine.getChart().on("click",function(params) {
	var year = params.name;
	var index = document.getElementById("queryForm:vol").selectedIndex;
	var voltage = document.getElementById("queryForm:vol").options[index].value;
	getTransDataYear(year,voltage);
	if ($("#yeardate").empty()) {
		$("#yeardate").append(year + "年变压器故障类型分析");
	} else {
		$("#yeardate").append("");
	}
	if ($("#electbarid").empty()) {
		$("#electbarid").append(year + "年南方电网变压器故障类型统计");
	} else {
		$("#electbarid").append("");
	}
})

//母线
function getMuxianBarsLine(datas) {
	var yrightname = '年故障率';
	var yleftname = '母线条数';
	var yaddname = '新增条数';
	var legendLabels = [ '母线条数', '新增条数', '年故障率' ];
	var ratios = [];
	//增加数
	var add = [];
	//故障次数
	var faultNum = [];
	//总数
	var totalNum = [];
	// 年份
	var myears = []
	for ( var i in datas) {
		for ( var j in datas[i].value) {
			if (datas[i].value[j].name == "母线条数") {
				totalNum.push(datas[i].value[j].value);
			}
			if (datas[i].value[j].name == "新增条数") {
				add.push(datas[i].value[j].value);
			}
			if (datas[i].value[j].name == "故障次数") {
				faultNum.push(datas[i].value[j].value);
			}
		}
		myears.push(datas[i].name);
	}
	for (var i = 0; totalNum.length > i; i++) {
		var ratio = 0;
		if(0 != totalNum[i]) {
			ratio = (parseInt(faultNum[i]) / parseInt(totalNum[i])) * 100;
		}
		ratios.push(ratio.toFixed(2));
	}
	var start = $("#firstYear1").val();
	var end = $("#lastYear1").val();
	$("#muxiandate").empty();
	$("#muxiandate").prepend(start + "-" + end + "年南方电网母线运行情况");
	if ($("#muxianpietitle").empty()) {
		$("#muxianpietitle").append(start + "年母线故障类型分析");
	} else {
		$("#muxianpietitle").append("");
	}
	if ($("#muxianbartitle").empty()) {
		$("#muxianbartitle").append(start + "年南方电网母线故障类型统计");
	} else {
		$("#muxianbartitle").append("");
	}

	muxianBarLine.setLeftyAxisTitle('条数');
	muxianBarLine.setRightyAxisTitle('年故障率（次/百条·年）');
	muxianBarLine.setLegendLabels(legendLabels);
	muxianBarLine.setNumber(totalNum);
	muxianBarLine.setRatio(ratios);
	muxianBarLine.setYears(myears);
	muxianBarLine.setYleftname(yleftname);
	muxianBarLine.setYrightname(yrightname);
	muxianBarLine.setYaddname(yaddname);
	muxianBarLine.setAdd(add);
	muxianBarLine.show();
}

//饼状图
var muxianfaultpie;
function getMuXianPie(datas) {
	if(datas[0].faultType.length==0) {
		$("#muxianpie").html("该时间暂无数据信息");
	} else {
		muxianfaultpie = new Pie("muxianpie");
		var faultTypeData = [];
		for ( var i in datas[0].faultType) {
			var num = [];
			num.push(datas[0].faultType[i]);
			num.push(0);
			faultTypeData.push(num);
		}
		for ( var i in faultTypeData) {
			for (var j = 1; datas.length > j; j++) {
				faultTypeData[i][1] += parseInt(datas[j].value[i]);
			}
		}
		var option = muxianfaultpie.getOption();
		option.legend.orient = 'vertical';
		muxianfaultpie.setLegendPosition('left', 'left');
		muxianfaultpie.setPie(faultTypeData);
		muxianfaultpie.setPieOuterRadius('50%');
		muxianfaultpie.show();
	}
}
//变压器故障类型统计
var muxianfaultTypeCompanyBar;
function getMuXianBar(datas) {
	if(datas[0].faultType.length==0) {
		$("#fault-muxian").html("该时间暂无数据信息");
	} else {
		muxianfaultTypeCompanyBar = new CategoryBar('fault-muxian');
		var faultTypeData = {
				name : '',
				xData : {
					name : '单位',
					value : []
				},
				yData : {
					name : '次数',
					value : []
				}
		};
		for (var i = 1; datas.length > i; i++) {
			faultTypeData.xData.value.push(datas[i].name);
		}
		;
		for ( var j in datas[0].faultType) {
			var obj = {};
			obj["lineValue"] = [];
			obj["lineName"] = datas[0].faultType[j];
			for (var i = 1; datas.length > i; i++) {
				obj["lineValue"].push(datas[i].value[j]);
			}
			faultTypeData.yData.value.push(obj);
		}
		var opt = muxianfaultTypeCompanyBar.getOption();
		opt.tooltip.axisPointer.type = "shadow";
		
//	opt.title.left = 'center'; // 标题位置
		opt.grid.top = '30%'; // 图形位置
//	opt.legend.top = '10%'; // 图标位置
//	opt.grid.bottom = 40;
		opt.legend.top = 10;
		opt.title.textStyle.fontSize = 16;
		opt.title.textStyle.fontWeight = 'normal';
		muxianfaultTypeCompanyBar.setTitlePosition('center').setDataZoomStartValue(0, 10);
		muxianfaultTypeCompanyBar.setData(faultTypeData);
		muxianfaultTypeCompanyBar.show();
	}
}

muxianBarLine.getChart().on("click",function(params) {
	var year = params.name;
	var index = document.getElementById("queryForm1:vol1").selectedIndex;
	var voltage = document.getElementById("queryForm1:vol1").options[index].value;
	getBusbarDataYear(year,voltage);
	if ($("#muxianpietitle").empty()) {
		$("#muxianpietitle").append(year + "年母线故障类型分析");
	} else {
		$("#muxianpietitle").append("");
	}
	if ($("#muxianbartitle").empty()) {
		$("#muxianbartitle").append(year + "年南方电网母线故障类型统计");
	} else {
		$("#muxianbartitle").append("");
	}
})

/*
function initdatas() {
	$.getJSON('/rest/transmission/getAllBreakerException',function(result) {
		var datas = [];
		for ( var i in result) {
			datas.push([ result[i].stationName,
					result[i].devType,
					result[i].vol + "kV",
					result[i].faultDevice,
					getTimeStr(result[i].faultTime),
					result[i].faultType,
					result[i].faultReason ]);
		}
		initBreakerExceptionTb(datas);
	});
}
function getTimeStr(time) {
	var t = new Date(time);
	return t.getFullYear() + "-" + doubleNum(t.getMonth() + 1)
			+ "-" + doubleNum(t.getDate()) + " "
			+ doubleNum(t.getHours()) + ":"
			+ doubleNum(t.getMinutes()) + ":"
			+ doubleNum(t.getSeconds());
}
function doubleNum(num) {
	return num > 9 ? num : '0' + num;
}

function initQueryYear() {
	var date = new Date();
	var currentYear = date.getFullYear();
	$('#exceptionYear_id').val(currentYear);
	$('#exceptionYear_id').datetimepicker({
		startView: 4,
		minView:4,
		autoclose : true,
		format : 'yyyy'
	});
}

function initExceptionDatas() {
    exceptionYear = $("#exceptionYear_id").val();
	$.getJSON('/rest/transmission/getBreakerExceptionByYear/' + exceptionYear, function(result) {
		var datas = [];
		for ( var i in result) {
			datas.push([ result[i].stationName,
				result[i].devType,
				result[i].vol + "kV",
				result[i].faultDevice,
				getTimeStr(result[i].faultTime),
				result[i].faultType,
				result[i].faultReason ]);
		}
		initBreakerExceptionTb(datas);
	});
}

var breakerExceptionTb;
var exceptionYear;
//开关设备异常
function initBreakerExceptionTb(datas) {
	if(breakerExceptionTb){
		breakerExceptionTb.getTable().destroy();
	}
	var columns = [ '一次设备检修范围', '设备类型', '电压等级', '故障设备', '故障时间',	'故障类型', '故障原因' ];
	var datatables = MyTable.getDefaultOptions();
	datatables.paging = true;
	breakerExceptionTb = new MyTable('sthtable', columns);
	breakerExceptionTb.setDataSet(datas);
	breakerExceptionTb.show();
}
*/